package com.tt.dao;

import java.awt.print.Pageable;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.tt.entity.Admin;
import com.tt.entity.Page;
import com.tt.entity.Strategy;
import com.tt.entity.User;
import com.tt.utils.MD5Util;

@Repository
@Transactional
public class AdminDaoImpl {
	@Resource
	private SessionFactory sessionFactory;
	//注册
	public int save(Admin admin){
		int save = (int) this.sessionFactory.getCurrentSession().save(admin);
		return save;
	}
	//登录
	public boolean loginAdmin(String username,String password){
		Admin a = null;
		String pass= MD5Util.encode2hex(password);
		String hql="from Admin s where s.username = ? and s.password = ?";
		try {
			Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
			query.setParameter(0, username);
			query.setParameter(1, password);
			a =(Admin) query.uniqueResult();
			
		} catch (Exception e) {
		}
		if (a != null) {
			return true;
		} else {
		return false;
	}
}
	//查看所有已发布拾光动态
	public Page<Strategy> findAll(int pageNum,int pageSize){
		String hql="from Strategy";
		String hql1="select count(id) from Strategy";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		Query query1 = this.sessionFactory.getCurrentSession().createQuery(hql1);
		Number number = (Number) query1.uniqueResult();
		int count=number.intValue();
		System.out.println(count);
		query.setFirstResult((pageNum-1)*pageSize);
		query.setMaxResults(pageSize);
		List<Strategy> list = query.list();
		Page<Strategy> page=new Page<>();
		page.setList(list);
		page.setPageSize(pageSize);
		page.setPageNum(pageNum);
		page.setTotalCount(count);
		return page;
	}
	//获取用户id
	public User save(int id){
		String hql = "from User where id=?";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0,id);
		User user=(User)query.uniqueResult();
        System.out.println("dao"+user);
		return user;
		
	}
	//删除发布拾光动态
   public void deleteStrategyt(int id) {
	   Strategy strategy = this.sessionFactory.getCurrentSession().load(Strategy.class, id);
	   this.sessionFactory.getCurrentSession().delete(strategy);
	}

   //查看所有用户
   public Page<User>findAll2(int pageNum,int pageSize){
	   String hql="from User";
		String hql1="select count(id) from User";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		Query query1 = this.sessionFactory.getCurrentSession().createQuery(hql1);
		Number number = (Number) query1.uniqueResult();
		int count=number.intValue();
		System.out.println(count);
		query.setFirstResult((pageNum-1)*pageSize);
		query.setMaxResults(pageSize);
		List<User> list = query.list();
		Page<User> page=new Page<>();
		page.setList(list);
		page.setPageSize(pageSize);
		page.setPageNum(pageNum);
		page.setTotalCount(count);
		return page;
   }
   //删除用户
   public void deleteUser(int id){	   
	   User user = this.sessionFactory.getCurrentSession().load(User.class, id);
	   this.sessionFactory.getCurrentSession().delete(user);
   }
   
   
 //显示升级为商家的用户信息
   public Page<User>findAll3(int pageNum,int pageSize,Integer uid){
		Query query = this.sessionFactory.getCurrentSession().createQuery("from User where status=?");
		Query query2 = this.sessionFactory.getCurrentSession().createQuery("select count(id) from User where status=?");
		query2.setParameter(0, uid);
		Number number = (Number) query2.uniqueResult();
		int count=number.intValue();
		System.out.println(count);
		query.setParameter(0, uid);
		query.setFirstResult((pageNum-1)*pageSize);
		query.setMaxResults(pageSize);
		List<User> list = query.list();
		Page<User> page=new Page<>(pageNum, pageSize);
		page.setList(list);
		page.setTotalCount(count);
		return page;
   }
   //更新数据库
   public void updateUser(User p) {
		this.sessionFactory.getCurrentSession().update(p);
	}
   public String select(int id){
		String hql = "from User where id=?";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0,id);
		String user=(String) query.uniqueResult();
		return user;
	}
   
		
	}
	
	
